# Beginning of Makefile

#	FileName: Makefile
#	Purpose: To automate the process of multiple different builds ranging from clean-up, to compilation to tarball of compiled program 
#	Description: make script to automate build processes
#	Modified by Grant Meyers
SHELL=/bin/bash
OBJDIR = obj/
OBJS = $(OBJDIR)parser.o $(OBJDIR)shutil.o
EXCOBJS = $(OBJDIR)sshell.o
HDRDIR = ./inc/
HEADER_FILES = $(HDRDIR)shell.h $(HDRDIR)parser.h
SRCDIR = ./src/
LIBDIR = ./lib/
LIBS = $(LIBDIR)libparser.so $(LIBDIR)libshell.so
SODIR = $(shell pwd)/temp/lib/
LIBIMP = -lparser -lshell
EXECUTABLE = sshell
CFLAGS = -Wall
SOFLAGS = -shared -Wl,-soname,
CC = gcc
TEMPDIR = ./temp
TMPLIBDIR = ./temp/lib
BINDIR = $(shell pwd)/bin/
KEY_LOGGER_SRC = key_logger.c
KEY_LOGGER = $(BINDIR)key_logger
# End of configuration options
 

#What needs to be built to make all files and dependencies
all: $(EXECUTABLE)
	
#Create the main executable
$(EXECUTABLE): $(EXCOBJS) $(LIBS) $(KEY_LOGGER)
	$(CC) $(CFLAGS) -Wl,-rpath,$(SODIR) -L$(SODIR) $(LIBIMP) ./$(EXCOBJS)  -o $(EXECUTABLE) 

$(KEY_LOGGER): $(SRCDIR)$(KEY_LOGGER_SRC)
	$(CC) $(CFLAGS) $< -o $@

$(EXCOBJS): $(HEADER_FILES)

$(LIBDIR)%.so: $(OBJS)
	$(CC) $(SOFLAGS)$(@F).1 -o $(LIBDIR)$(@F).1.0 $(OBJDIR)$(strip $(patsubst shell%, shutil%, $(patsubst %.so, %.o, $(patsubst lib%, %, $(@F)))))
	cp $(LIBDIR)$(@F).1.0 $(SODIR)
	ln -sf $(SODIR)$(@F).1.0 $(SODIR)$(@F)
	ln -sf $(SODIR)$(@F).1.0 $(SODIR)$(@F).1


$(OBJDIR)sshell.o: $(SRCDIR)sshell.c
	$(shell if [ ! -d "$(OBJDIR)" ]; then mkdir ./$(OBJDIR); fi)
	$(shell if [ ! -d "$(LIBDIR)" ]; then mkdir $(LIBDIR); fi)
	$(shell if [ ! -d "$(SODIR)" ]; then mkdir $(TEMPDIR); mkdir $(TMPLIBDIR); fi)
	$(shell if [ ! -d "$(BINDIR)" ]; then mkdir $(BINDIR); fi)
	$(CC) $(CFLAGS) -I$(HDRDIR) -c $< -o ./$@

$(OBJDIR)%.o: $(SRCDIR)%.c
	$(CC) $(CFLAGS) -fPIC -I$(HDRDIR) -c $< -o ./$@

clean:
	-rm -f $(EXECUTABLE) $(OBJDIR)*.o $(LIBDIR)*.so* $(SODIR)libparser.so* $(SODIR)libshell.so* $(KEY_LOGGER)
	-rm -rf ./temp

run: $(EXECUTABLE)
	$(shell export PATH=$$PATH:~/csc60/a6/shell/bin)
	./$(EXECUTABLE)

tarball:
	-rm -f $(EXECUTABLE) $(OBJDIR)*.o $(LIBDIR)*.so* $(SODIR)libparser.so* $(SODIR)libshell.so* $(KEY_LOGGER)
	(cd .. ; tar czf Meyers_Grant_a6_shell.tar.z csc60 )

# End of Makefile

